<?php

namespace app\admin\controller;
use app\BaseController;
use GuzzleHttp\Client;
class Python extends BaseController
{
    /**
     * @deprecated 抓取网站数据
     * @url https://coserlab.io
     * @return \think\response\Json
     */
    public function coserlab()
    {
        $client = new Client([
            'verify' => false
        ]);
        $page = $this->request->get('page');
        // type 1:cosplay 5:写真
        $type = $this->request->get('type', 1);
        $result = $client->request('GET','https://coserlab.io/wp-json/wp/v2/posts?categories='.$type.'&per_page=10&page='.$page);
        $result = json_decode($result->getBody()->getContents(),true);
        return result()->success($result);
    }
    /**
     * @deprecated 抓取tujigu列表
     * @url https://www.tujigu.top
     * @return \think\response\Json
     */
    public function tujigu() {
        $client = new Client([
            'verify' => false
        ]);
        $page = $this->request->get('page', 1);
        $result = $client->request('GET','http://localhost:8885/Reptile/list?page='.$page);
        $result = json_decode($result->getBody()->getContents(),true);
        return result()->success($result['data']);
    }
    /**
     * @deprecated 抓取tujigu详情
     * @url https://www.tujigu.top
     * @return \think\response\Json
     */
    public function tujiguImages() {
        $client = new Client([
            'verify' => false
        ]);
        $link = $this->request->post('link');
        $response = $client->post("http://localhost:8885/Reptile/detail", [
            'json' => [
                'url' => $link
            ]
        ]);
        $result = json_decode($response->getBody()->getContents(),true);
        return result()->success($result['data']);
    }
    /**
     * @deprecated tujigun保存到本地
     */
    public function saveListTuji()
    {
        $client = new Client([
            'verify' => false
        ]);
        $page = $this->request->get('page', 1);
        $result = $client->request('GET','http://localhost:8885/Reptile/list?page='.$page);
        $result = json_decode($result->getBody()->getContents(),true);
        $data = $result['data'];
        foreach ($data as $key => $item) {
            $this->saveDetailTuji($item, $page.'-'.$key.'-');
        }
        return result()->success('保存成功');
    }
    private function saveDetailTuji($item, $append)
    {
        $client = new Client([
            'verify' => false
        ]);
        $link = $item['href'];
        $response = $client->post("http://localhost:8885/Reptile/detail", [
            'json' => [
                'url' => $link
            ]
        ]);
        $result = json_decode($response->getBody()->getContents(),true);
        $item['images'] = $result['data'];
        array_unshift($item['images'], ['image' => $item['cover']]);
        $this->saveImgTuji($item, $append);
    }
    private function saveImgTuji($item, $append)
    {

        $folderPath = "tujigu/" . $append . $item['title'];
        $fullFolderPath = public_path($folderPath);
        if (!file_exists($fullFolderPath)) {
            mkdir($fullFolderPath, 0777, true);
        }
        //  保存图片到文件夹
        foreach ($item['images'] as $key => $row) {
            $val = $row['image'];
            $arr = explode('?', $row['image']);
            $fileName = basename($arr[0]);
            $savePath = $folderPath . '/' . $fileName;
            //  判断文件是否存在
            if (file_exists($savePath)) {
                continue;
            }
//            $headers = get_headers($val);
//            if (strpos($headers[0], '200') === false) {
//                //已失效
//            }
            $randomIP = getRandomIP();
            try {
                $options = [
                    'ssl' => [
                        'verify_host'      => false,
                        'verify_peer'      => false,
                        'verify_peer_name' => false,
                    ],
                    'http' => [
                        'header' => "User-Agent: Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Mobile Safari/537.36 Edg/120.0.0.0\r\n" .
                            "X-Forwarded-For: $randomIP\r\n",
                    ],
                ];
                $context = stream_context_create($options);
                file_put_contents($savePath, file_get_contents($val, false, $context));
            } catch (\Throwable $th) {
                //throw $th;
            }
        }
    }

    /**
     * @deprecated 抓取fulitu列表
     * @url https://www.fulitu.top
     * @return \think\response\Json
     */
    public function fulitu() {
        $client = new Client([
            'verify' => false
        ]);
        $page = $this->request->get('page', 1);
        $name = input('name','wanghong');
        $result = $client->request('GET','http://localhost:8885/Reptile/fulituList?page='.$page.'&name='.$name);
        $result = json_decode($result->getBody()->getContents(),true);
        return result()->success($result['data']);
    }

    public function getPage()
    {
        $client = new Client([
            'verify' => false
        ]);
        $link = $this->request->post('link');
        $response = $client->post("http://localhost:8885/Reptile/pageCount", [
            'json' => [
                'link' => $link
            ]
        ]);
        $result = json_decode($response->getBody()->getContents(),true);
        return result()->success($result['data']);
    }

    public function fulituImages()
    {
        $client = new Client([
            'verify' => false
        ]);
        $link = $this->request->post('link');
        $response = $client->post("http://localhost:8885/Reptile/fulituDetail", [
            'json' => [
                'link' => $link
            ]
        ]);
        $result = json_decode($response->getBody()->getContents(),true);
        return result()->success($result['data']);
    }

    public function saveListFulitu()
    {
        $images = $this->request->post('images');
        $title = $this->request->post('title');
        $folderPath = "fulitu/" . $title;
        $fullFolderPath = public_path($folderPath);
        if (!file_exists($fullFolderPath)) {
            mkdir($fullFolderPath, 0777, true);
        }
        foreach ($images as $key => $item) {
            $arr = explode('?', $item);
            $fileName = basename($arr[0]);
            $savePath = $folderPath . '/' . $fileName;
            //  判断文件是否存在
            if (file_exists($savePath)) {
                continue;
            }
            $randomIP = getRandomIP();
            try {
                $options = [
                    'ssl' => [
                        'verify_host'      => false,
                        'verify_peer'      => false,
                        'verify_peer_name' => false,
                    ],
                    'http' => [
                        'header' => "User-Agent: Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Mobile Safari/537.36 Edg/120.0.0.0\r\n" .
                            "X-Forwarded-For: $randomIP\r\n",
                    ],
                ];
                $context = stream_context_create($options);
                file_put_contents($savePath, file_get_contents($item, false, $context));
            } catch (\Throwable $th) {
                //throw $th;
            }
        }
        return result()->success('保存成功');
    }

    /**
     * @deprecated 抓取jk列表
     * @url https://www.jk.rs
     * @return \think\response\Json
     */
    public function jk()
    {
        $client = new Client([
            'verify' => false
        ]);
        $page = input('page',1);
        $name = input('name','JK');
        $result = $client->request('GET','http://localhost:8885/Reptile/jkList?page='.$page.'&name='.$name);
        $result = json_decode($result->getBody()->getContents(),true);
        return result()->success($result['data']);
    }

    public function jkImages()
    {
        $client = new Client([
            'verify' => false
        ]);
        $link = $this->request->post('link', 'https://www.jk.rs/2024/03/19/583.html');
        $response = $client->post("http://localhost:8885/Reptile/jkDetail", [
            'json' => [
                'link' => $link
            ]
        ]);
        $result = json_decode($response->getBody()->getContents(),true);
        return result()->success($result['data']);
    }

    public function saveListJk()
    {
        $images = $this->request->post('images');
        $title = $this->request->post('title');
        $folderPath = "jk/" . $title;
        $fullFolderPath = public_path($folderPath);
        if (!file_exists($fullFolderPath)) {
            mkdir($fullFolderPath, 0777, true);
        }
        foreach ($images as $key => $item) {
            $arr = explode('?', $item);
            $fileName = basename($arr[0]);
            $savePath = $folderPath . '/' . $fileName;
            //  判断文件是否存在
            if (file_exists($savePath)) {
                continue;
            }
            $randomIP = getRandomIP();
            try {
                $options = [
                    'ssl' => [
                        'verify_host'      => false,
                        'verify_peer'      => false,
                        'verify_peer_name' => false,
                    ],
                    'http' => [
                        'header' => "User-Agent: Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Mobile Safari/537.36 Edg/120.0.0.0\r\n" .
                            "X-Forwarded-For: $randomIP\r\n",
                    ],
                ];
                $context = stream_context_create($options);
                file_put_contents($savePath, file_get_contents($item, false, $context));
            } catch (\Throwable $th) {
                //throw $th;
            }
        }
        return result()->success('保存成功');
    }
}
